package com.huang.app

import android.app.Application
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ProcessUtils
import com.blankj.utilcode.util.Utils
import com.bumptech.glide.Glide
import com.huang.mvvm.control.ControlConfig
import com.huang.mvvm.control.ControlProvider
import com.huang.network.NetworkManager
import com.tencent.mmkv.MMKV
import me.yokeyword.fragmentation.Fragmentation

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        /** 这个是防止多进程时，多次初始化App*/
        if (packageName != ProcessUtils.getCurrentProcessName()) return

        instance = this

        /** 初始化工具类 */
        Utils.init(this)

        MMKV.initialize(this)

        if (BuildConfig.DEBUG) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)

        NetworkManager.init(Host.DEFAULT_HOST, {
            null
        }, {
            null
        }, {
            null
        })

        Fragmentation.builder()
            .stackViewMode(Fragmentation.SHAKE)
            .debug(BuildConfig.DEBUG)
            .install()

        ControlProvider.setGlobalConfig(ControlConfig(AppControllable::class))
    }


    override fun onTrimMemory(level: Int) {
        Glide.get(this).onTrimMemory(level)
        super.onTrimMemory(level)
    }

    override fun onLowMemory() {
        Glide.get(this).onLowMemory()
        super.onLowMemory()
    }

    override fun onTerminate() {
        super.onTerminate()
        ARouter.getInstance().destroy()
    }

    companion object {
        var instance: App? = null
            private set
    }
}